package com.gdut.simple.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.data.entity.Note;
import com.gdut.simple.view.activity.WebActivity;
import com.gdut.simple.view.activity.explore.ResourceActivity;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.adapter.base.AbsViewHolder;

import java.util.List;

public class NoteAdapter extends AbsRecyclerAdapter<Note> {

    public NoteAdapter(Context context, List<Note> list) {
        super(context, list);
    }

    @Override
    protected AbsViewHolder createHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(mInflater.inflate(R.layout.layout_note_item, parent, false));
    }

    @Override
    protected void showViewHolder(AbsViewHolder holder, final int position) {
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.type.setText(mData.get(position).getType());
        final WikiAdapter adapter = new WikiAdapter(mContext, mData.get(position).getWiki());
        viewHolder.wiki.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        viewHolder.wiki.setAdapter(adapter);
        adapter.setOnItemClickListener(new DefaultItemClickListener() {
            @Override
            public void onClick(View view, int pos) {
                Intent intent = new Intent(mContext, WebActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", "Wiki");
                bundle.putString("url", mData.get(position).getWiki().get(pos).getUrl());
                bundle.putString("title", mData.get(position).getWiki().get(pos).getTitle());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    static class ItemViewHolder extends AbsViewHolder {

        TextView type;
        RecyclerView wiki;

        public ItemViewHolder(View itemView) {
            super(itemView);
            type = (TextView) itemView.findViewById(R.id.id_note_type);
            wiki = (RecyclerView) itemView.findViewById(R.id.id_note_wiki_list);
        }
    }

}
